package com.ddpt.permission.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lh.center.common.base.BizServiceImpl;
import com.lh.center.permission.api.entity.Permission;
import com.lh.center.permission.api.entity.RolePermission;
import com.lh.center.permission.mapper.RolePermissionMapper;
import com.lh.center.permission.service.RolePermissionService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色-权限关联表 服务实现类
 * </p>
 *
 * @author czy
 * @since 2022-04-07
 */
@Service
public class RolePermissionServiceImpl extends BizServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    @GlobalTransactional
    public void addBatch(Long roleId, List<Long> permissionIds) {
        List<RolePermission> permissions = new ArrayList<>();
        RolePermission cur;
        for(Long permissionId : permissionIds) {
            cur = new RolePermission();
            cur.setRoleId(roleId);
            cur.setPermissionId(permissionId);
            permissions.add(cur);
        }
        saveBatch(permissions);
    }

    @Override
    @GlobalTransactional
    public void updateBatch(Long roleId, List<Long> permissionIds) {
        remove(Wrappers.lambdaQuery(RolePermission.class).eq(RolePermission::getRoleId,roleId));
        addBatch(roleId,permissionIds);
    }

    @Override
    public List<Permission> queryPermissionByRole(Long roleId) {
        return rolePermissionMapper.queryPermissionByRole(roleId);
    }
}
